import Flutter
import UIKit
import Contacts
import AVFoundation
import AdSupport
import AppTrackingTransparency
import Photos

@available(iOS 13, *)
public class IosSystemPermissionPlugin: NSObject, FlutterPlugin {
    public static func register(with registrar: FlutterPluginRegistrar) {
        let channel = FlutterMethodChannel(name: "ios_system_permission", binaryMessenger: registrar.messenger())
        let instance = IosSystemPermissionPlugin()
        registrar.addMethodCallDelegate(instance, channel: channel)
    }
    
    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        switch call.method {
        case "getContactPermission":
            result(getContactPermission())
        case "getPhotoPermission":
            result(getPhotoPermission())
        case "getTrackingPermission":
            result(getTrackingPermission())
        case "requestContactPermission":
            Task {
                result(await requestContactPermission())
            }
        case "requestPhotoPermission":
            Task {
                result(await requestPhotoPermission())
            }
        case "requestTrackingPermission":
            Task {
                result(await requestTrackingPermission())
            }
            
        default:
            result(FlutterMethodNotImplemented)
        }
    }
    
    public func getContactPermission()-> Int{
        return CNContactStore.authorizationStatus(for: .contacts).rawValue
    }
    
    public func getPhotoPermission()-> Int{
        return PHPhotoLibrary.authorizationStatus().rawValue
    }
    
    
    public func getTrackingPermission()-> Int{
        
        if #available(iOS 14.0, *) {
            if let intValue = Int(exactly:ATTrackingManager.trackingAuthorizationStatus.rawValue) {
                return intValue
            } else {
                return -1
            }
        } else {
            return 3
        }
        
    }
    
    public func requestContactPermission() async -> Int{
        
        var status = -1
        
        await CNContactStore().requestAccess(for: .contacts) { (granted, error) in
            if granted {
                status = 3
            } else {
                status = 2
            }
        }
        return status
        
        
    }
    
    public func requestPhotoPermission()async -> Int{
        var status1 = -1
        
        await PHPhotoLibrary.requestAuthorization { status in
            status1 = status.rawValue
        }
        
        return status1;
    }
    
    
    public func requestTrackingPermission() async -> Int{
        
        var status1 = -1
        
        if #available(iOS 14.0, *) {
            
            await ATTrackingManager.requestTrackingAuthorization { status in
                status1 = Int(exactly:status.rawValue)!
            }
            
            return status1
            
        } else {
            return 3
        }
        
    }
    
}
